

/*------------------------------------------------------------------------------

Input: Semi-raw data (some cleaning has already been done.)
Output: Clean regulatory filings, ready to be analyzed.

------------------------------------------------------------------------------*/

use "$source/BDD_DA_stable_may2017", clear

xtset matricule year

* drop a tiny insurer

drop if entite=="MACSF_PREVOYANCE"

* dummy bancassureur

gen bancassureur = entite=="ASSUVIE" | entite =="ASSURANCES_DU_CREDIT_MUTUEL_VIE_SA" | entite =="HSBC_ASSURANCES_VIE__France_" | entite=="SERENIS_VIE" ///
	| entite=="ECUREUIL_VIE"  | entite=="ASSURANCES_BANQUE_POPULAIRE_VIE" | entite=="ASSURANCES_DU_CREDIT_MUTUEL_VIE"  | entite=="CNP_ASSURANCES" ///
	| entite=="SOGECAP"  | entite=="CARDIF_ASSURANCE_VIE" | entite=="PREDICA"  | entite=="NATIXIS_ASSURANCES_PARTENAIRES" | entite=="ASSURANCES_CREDIT_MUTUEL_NORD_VIE"



*** Account value

* NB: Use data from C1 templace because it is pro forma (both PM_ouv and PM_clo take into account acquisitions and contract repurchases occurring during the year)

* individual & group contracts (cat 1 2 4 5 7) at year start (ouv) and year end (clo)

gen PM_ouv_ig = pm_ouv_cat1 + pm_ouv_cat2 + pm_ouv_cat4 + pm_ouv_cat5 + pm_ouv_cat7
gen PM_clo_ig = pm_clo_cat1 + pm_clo_cat2 + pm_clo_cat4 + pm_clo_cat5 + pm_clo_cat7

* jumps in account value

gen alpha_ig = PM_ouv_ig/l.PM_clo_ig



*** Contract return

gen Revalo_ig = (it_pm_cat1+it_prest_cat1+pb_pm_cat1+pb_prest_cat1) + (it_pm_cat2+it_prest_cat2+pb_pm_cat2+pb_prest_cat2) + (it_pm_cat4+it_prest_cat4+pb_pm_cat4+pb_prest_cat4) + (it_pm_cat5+it_prest_cat5+pb_pm_cat5+pb_prest_cat5) + (it_pm_cat7+it_prest_cat7+pb_pm_cat7+pb_prest_cat7)



*** Asset value

* lag asset value is =0 for entrants in entry year --> assign missing (7 obs)

egen firstyear = min(year), by(entite)
gen lag_is_zero = repr_prov_vnc_N3BJlag==0
foreach x in repr_prov_vnc_N3BJlag repr_prov_vm_N3BJlag actifs_vnc_uc_N3BJ_lag actifs_vm_uc_N3BJ_lag tot_plac_vnc_N3BJ_lag tot_plac_vm_N3BJ_lag {
replace `x' = . if year==firstyear & lag_is_zero==1
}
drop firstyear lag_is_zero

* book value of assets

gen Asset_book = repr_prov_vnc_N3BJ - actifs_vnc_uc_N3BJ

* market value of assets

gen Asset_mrkt = repr_prov_vm_N3BJ - actifs_vm_uc_N3BJ

* same at year-start

gen Asset_book_lag = repr_prov_vnc_N3BJlag - actifs_vnc_uc_N3BJ_lag
gen Asset_mrkt_lag = repr_prov_vm_N3BJlag - actifs_vm_uc_N3BJ_lag



*** Asset portfolio allocation

* stocks
gen Asset_stock_mrkt = act_vm + opcvm_act_vm

* bonds
gen Asset_bond_mrkt = oblig_vm + opcvm_oblig_vm

* real_estate
gen Asset_re_mrkt = immo_vm

* loans
gen Asset_loan_mrkt = pret_vm + autre_pret_vm

* cash
gen Asset_cash_mrkt = depot_vm + autre_depot_vm

* total assets
gen Asstot_mrkt = tot_plac_vm_N3BJ - actifs_vm_uc_N3BJ




*** Reserves

* Profit-sharing reserves

* year-end
gen Reserve_PPB_ig = ppb_clo_cat1 + ppb_clo_cat2 + ppb_clo_cat4 + ppb_clo_cat5 + ppb_clo_cat7

* year-start
gen Reserve_PPB_ig_lag = ppb_ouv_cat1 + ppb_ouv_cat2 + ppb_ouv_cat4 + ppb_ouv_cat5 + ppb_ouv_cat7


* Capitalization reserves

* year-end
gen Reserve_RC_ig = RC

* year-start
gen Reserve_RC_ig_lag = alpha_ig*l.RC


* Unrealized gains

* year-end
gen Reserve_unrealized = Asset_mrkt - Asset_book

* year-start
gen Reserve_unrealized_lag = Asset_mrkt_lag - Asset_book_lag


* Total reserves

* year-end
gen Reserve_ig = Reserve_PPB_ig + Reserve_RC_ig + Reserve_unrealized

* year-start
gen Reserve_ig_lag = Reserve_PPB_ig_lag + Reserve_RC_ig_lag + Reserve_unrealized_lag



*** Asset return

* asset yield (dividends + bond yield)

gen Retass_yield_ig = prod_placem_nets_cat1 + prod_placem_nets_cat2 + prod_placem_nets_cat4 + prod_placem_nets_cat5 + prod_placem_nets_cat7

* realized gains on bonds (= change in capitalization reserves)

gen Retass_dRC_ig = Reserve_RC_ig - Reserve_RC_ig_lag

* unrealized gains

gen Retass_unrea = Asset_mrkt - Asset_book - (Asset_mrkt_lag - Asset_book_lag)

* total asset return

gen Retass_ig = Retass_yield_ig + Retass_dRC_ig + Retass_unrea



*** Flow

* inflow (premiums)

gen Inflow_ig = prim_net_cat1 + prim_net_cat2 + prim_net_cat4 + prim_net_cat5 + prim_net_cat7

* redemptions

gen Redemp_ig = rachats_cat1 + rachats_cat2 + rachats_cat4 + rachats_cat5 + rachats_cat7

* total outflow

gen Outflow_ig = charge_prest_cat1 + charge_prest_cat2 + charge_prest_cat4 + charge_prest_cat5 + charge_prest_cat7

* involuntary outflow (total outflows - redemptions)

gen Forced_ig = Outflow_ig - Redemp_ig



*** Drop a few missing observations

foreach x in Reserve_ig Retass_ig {

* GAN_PREVOYANCE 2008
replace `x' = . if entite=="GAN_PREVOYANCE" & year==2008

* GENERALI_ASSURANCES_VIE 2004
replace `x' = . if entite=="GENERALI_ASSURANCES_VIE" & year==2004

}

* ARIAL_ASSURANCE 2002: entry year but PM_ig=0
drop if entite=="ARIAL_ASSURANCE" & year==2002



*** Equity

* NB: from BILPV template

* use the notion of equity such that 100% of the capitalization reserve belongs to shareholders

gen Equity = cap_propres_BILPV - resultat_ex_BILPV

* regulatory capital

rename total_A_C6EEV marge_A


*** Bring in inflation (source: Eurostat website)

merge m:1 year using "$source/desinflator", keep(1 3) nogen

save "$output/DA", replace




/*------------------------------------------------------------------------------

Prepare the regression sample.

------------------------------------------------------------------------------*/

use "$output/DA", clear
xtset matricule year

* contract return
gen Pa_ig = PM_ouv_ig + (Inflow_ig-Outflow_ig)/2
gen y = Revalo_ig/Pa_ig

* reserves at year start
* normalized by mid-point account value
gen res0 = (Reserve_PPB_ig_lag + Reserve_RC_ig_lag + Reserve_unrealized_lag)/Pa_ig if abs(alpha_ig-1)<.05
* normalized by year-start account value (i.e., truly known at year start)
gen res00 = (Reserve_PPB_ig_lag + Reserve_RC_ig_lag + Reserve_unrealized_lag)/PM_ouv_ig if abs(alpha_ig-1)<.05

* asset return
gen r = Retass_ig/Pa_ig

* reserves at year end before distribution
gen res1 = res0 + r

* equity
gen cap = l.Equity/PM_ouv_ig

* flows
foreach x in ig {
gen inflow_`x' = Inflow_`x'/Pa_`x'
gen redemp_`x' = Redemp_`x'/Pa_`x'
gen outflow_`x' = Outflow_`x'/Pa_`x'
gen forced_`x' = outflow_`x'-redemp_`x'
gen flow_`x' = inflow_`x'-outflow_`x'
}
gen loginflow_ig = log(1+Inflow_ig)

* lag asset return
gen lr = l.r
gen l2r = l3.r
gen l3r = l2.r

* lag contract return
gen ly = l.y

* lag portfolio share
foreach x in re stock bond cash loan {
gen lshare_`x' = l.Asset_`x'_mrkt/l.Asstot_mrkt
gen l2share_`x' = l2.Asset_`x'_mrkt/l2.Asstot_mrkt
gen l3share_`x' = l3.Asset_`x'_mrkt/l3.Asstot_mrkt
gen l4share_`x' = l4.Asset_`x'_mrkt/l4.Asstot_mrkt
}

* regression weight
keep if res1<.
egen n = count(year), by(matricule)
drop if n==1
drop n
egen w = sum(Pa_ig), by(year)
replace w = Pa_ig/w

* winsorize flows
foreach x in flow inflow redemp forced outflow {
foreach y in ig {
winsor `x'_`y'
}
}

* bring data on new investors
merge 1:1 matricule year using "$source/C20C", keepusing(nbsub_ig) keep(1 3) nogen 	// output/C20C
winsor nbsub_ig

save "$output/DA_reg", replace
